var mahjongSprites = [];  //初始化所有麻将

cc.Class({
    extends: cc.Component,

    properties: {
        start_btn:{
            default: null,
            type: cc.Button
        },
        peng_btn:{
            default: null,
            type: cc.Button
        },
        chi_btn:{
            default: null,
            type: cc.Button
        },
        gang_btn:{
            default: null,
            type: cc.Button
        },
        hu_btn:{
            default: null,
            type: cc.Button
        },
        guo_btn:{
            default: null,
            type: cc.Button
        },
        _sides:null,    //属于哪一方
        _pres:null,     //资源选择
        _foldPres:null, //TODO
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad :function() {
        if(!cc.sys.isNative && cc.sys.isMobile){
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }

        this._sides = ["myself","right","up","left"];
        this._pres = ["M_","R_","B_","L_"];
        this._foldPres = ["B_","R_","B_","L_"];

        cc.mahjongmgr = this; 

        //加载所有麻将资源
        //筒
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("dot_" + i);        
        }
        
        //条
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("bamboo_" + i);
        }
        
        //万
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("character_" + i);
        }
        
        //中、发、白
        mahjongSprites.push("red");
        mahjongSprites.push("green");
        mahjongSprites.push("white");
        
        //东西南北风
        mahjongSprites.push("wind_east");
        mahjongSprites.push("wind_west");
        mahjongSprites.push("wind_south");
        mahjongSprites.push("wind_north");

        //TODO initialize
        this.initView();
    },

    initView:function(){
        //初始化棋盘
        var game = this.node.getChildByName("game");
        var loading = this.node.getChildByName("WaitingConnection");
        var infobar = this.node.getChildByName("infobar");

        //显示没开局状态
        var init_bar = game.getChildByName("init");
        var start_bar = game.getChildByName("start");
        var opt_bar = game.getChildByName("opt");

        loading.active = false;
        init_bar.active = true;
        start_bar.active = false;
        opt_bar.active = false;
    },

    onStart :function(event) {
        //开始
       console.log("start szmj");

    },

    onPeng :function(event) {
        //碰
       console.log("peng");

    },

    onChi :function(event) {
        //吃
       console.log("chi");

    },

    onGang :function(event) {
        //杠
       console.log("gang");

    },

    onHu :function(event) {
        //胡
       console.log("hu");

    },

    onGuo :function(event) {
        //过
       console.log("guo");

    },

    getMahjongSpriteByID:function(id){
        return mahjongSprites[id];
    },
    
    getMahjongType:function(id){
      if(id >= 0 && id < 9){
          return 0;
      }
      else if(id >= 9 && id < 18){
          return 1;
      }
      else if(id >= 18 && id < 27){
          return 2;
      }
    },
    
    getSpriteFrameByMJID:function(pre,mjid){
        var spriteFrameName = this.getMahjongSpriteByID(mjid);
        spriteFrameName = pre + spriteFrameName;
        if(pre == "M_"){
            return this.bottomAtlas.getSpriteFrame(spriteFrameName);            
        }
        else if(pre == "B_"){
            return this.bottomFoldAtlas.getSpriteFrame(spriteFrameName);
        }
        else if(pre == "L_"){
            return this.leftAtlas.getSpriteFrame(spriteFrameName);
        }
        else if(pre == "R_"){
            return this.rightAtlas.getSpriteFrame(spriteFrameName);
        }
    },
    
    getAudioURLByMJID:function(id){
        var realId = 0;
        if(id >= 0 && id < 9){
            realId = id + 21;
        }
        else if(id >= 9 && id < 18){
            realId = id - 8;
        }
        else if(id >= 18 && id < 27){
            realId = id - 7;
        }
        return "nv/" + realId + ".mp3";
    },
    
    getEmptySpriteFrame:function(side){
        if(side == "up"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_up");
        }   
        else if(side == "myself"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_bottom");
        }
        else if(side == "left"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_left");
        }
        else if(side == "right"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_right");
        }
    },
    
    getHoldsEmptySpriteFrame:function(side){
        if(side == "up"){
            return this.emptyAtlas.getSpriteFrame("e_mj_up");
        }   
        else if(side == "myself"){
            return null;
        }
        else if(side == "left"){
            return this.emptyAtlas.getSpriteFrame("e_mj_left");
        }
        else if(side == "right"){
            return this.emptyAtlas.getSpriteFrame("e_mj_right");
        }
    },
    
    sortMJ:function(mahjongs,dingque){
        var self = this;
        mahjongs.sort(function(a,b){
            if(dingque >= 0){
                var t1 = self.getMahjongType(a);
                var t2 = self.getMahjongType(b);
                if(t1 != t2){
                    if(dingque == t1){
                        return 1;
                    }
                    else if(dingque == t2){
                        return -1;
                    }
                }
            }
            return a - b;
        });
    },
    
    getSide:function(localIndex){
        return this._sides[localIndex];
    },
    
    getPre:function(localIndex){
        return this._pres[localIndex];
    },
    
    getFoldPre:function(localIndex){
        return this._foldPres[localIndex];
    }
});
